using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using Common;
using CustomChart;
using Infragistics.UltraChart.Data.Series;
using Infragistics.UltraChart.Resources.Appearance;

namespace MainView.MainControls
{
	public class UCTemperatureChartEx : UCTemperatureChart
	{
		private class TemperatureData
		{
			public TimeSpan Time { get; set; }

			public double[] Temperature { get; set; }
		}

		public static readonly RoutedEvent OnCursorChangedRoutedEvent = EventManager.RegisterRoutedEvent("OnCursorChangedRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<RoutedEventArgs>), typeof(UCTemperatureChartEx));

		public static readonly RoutedEvent OnHighLightChangedRoutedEvent = EventManager.RegisterRoutedEvent("OnHighLightChangedRoutedEvent", RoutingStrategy.Bubble, typeof(EventHandler<HighLightRoutedEventArgs>), typeof(UCTemperatureChartEx));

		private List<TemperatureData> listTempData = new List<TemperatureData>();

		private List<double> listTwoTimeInterver = new List<double>();

		private int interval = 1;

		private XYSeriesEx[] series = new XYSeriesEx[96];

		private XYSeriesEx seriesCursor = new XYSeriesEx();

		private ToolStripMenuItem itemShowCursor = new ToolStripMenuItem();

		private Color defaultColor = Color.Chocolate;

		private bool isMouseDown;

		private XYSeriesEx seriesThreshold;

		private DateTime startDate = new DateTime(1, 1, 1, 0, 0, 0);

		public event RoutedEventHandler OnCursorChanged
		{
			add
			{
				AddHandler(OnCursorChangedRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnCursorChangedRoutedEvent, value);
			}
		}

		public event RoutedEventHandler OnHighLightChanged
		{
			add
			{
				AddHandler(OnHighLightChangedRoutedEvent, value);
			}
			remove
			{
				RemoveHandler(OnHighLightChangedRoutedEvent, value);
			}
		}

		private void RaiseCursorChangedChanged()
		{
			if (seriesCursor.Visible)
			{
				RaiseEvent(new RoutedEventArgs
				{
					RoutedEvent = OnCursorChangedRoutedEvent
				});
			}
		}

		public void RaiseHighLightChanged(int index)
		{
			RaiseEvent(new HighLightRoutedEventArgs
			{
				WellIndex = index,
				RoutedEvent = OnHighLightChangedRoutedEvent
			});
		}

		public UCTemperatureChartEx()
		{
			base.Chart.OnAxixRangeChanged += Chart_OnAxixRangeChanged;
			base.Chart.OnGetTooltipsArgs += Chart_OnGetTooltipsArgs;
			for (int i = 0; i < series.Count(); i++)
			{
				series[i] = new XYSeriesEx();
				series[i].PEs.Add(new PaintElement());
				series[i].PEs[0].Fill = defaultColor;
				series[i].Visible = false;
				base.Chart.AddDataSeries(series[i]);
			}
			scrollBar.Visibility = Visibility.Collapsed;
			seriesCursor.PEs.Add(new PaintElement());
			seriesCursor.PEs[0].Fill = Color.Blue;
			seriesCursor.Points.Add(new XYDataPoint(new TimeSpan(0, 1, 0).TotalSeconds, -10.0, string.Empty, false));
			seriesCursor.Points.Add(new XYDataPoint(new TimeSpan(0, 1, 0).TotalSeconds, 200.0, string.Empty, false));
			seriesCursor.Visible = false;
			seriesCursor.Key = "Cursor";
			base.Chart.AddThresholdSeries(seriesCursor);
			ToolStripMenuItem toolStripMenuItem = new ToolStripMenuItem();
			toolStripMenuItem.Text = "Show Block Temperature Series";
			toolStripMenuItem.Checked = true;
			toolStripMenuItem.Click += menuItem_Click;
			base.Chart.AddMenuItem(toolStripMenuItem);
			itemShowCursor.Text = "Show Cursor";
			itemShowCursor.Checked = false;
			itemShowCursor.Click += itemShowCursor_Click;
			base.Chart.AddMenuItem(itemShowCursor);
			base.Chart.OnSelectedSeriesChanged += Chart_OnSelectedSeriesChanged;
			base.Chart.Chart.MouseDown += Chart_MouseDown;
			base.Chart.Chart.MouseMove += Chart_MouseMove;
			base.Chart.Chart.MouseUp += Chart_MouseUp;
			base.Chart.Chart.SizeChanged += Chart_SizeChanged;
		}

		private void Chart_OnGetTooltipsArgs(object sender, GetTooltipsArgs e)
		{
			if (isMouseDown)
			{
				e.Handle = true;
			}
			else if (e.Series != null && e.Series is XYSeriesEx)
			{
				int num = series.ToList().IndexOf(e.Series as XYSeriesEx);
				if (num != -1)
				{
					StringBuilder stringBuilder = new StringBuilder();
					stringBuilder.AppendLine(Utility.GetMessage("well_no") + Utility.GetMessage("Colon") + ((char)(65 + num / 12)).ToString() + (num % 12 + 1));
					stringBuilder.AppendLine(base.Chart.GetTitleX() + Utility.GetMessage("Colon") + "#VAL{X}");
					stringBuilder.AppendLine(base.Chart.GetTitleY() + Utility.GetMessage("Colon") + "#VALX{Y}");
					e.Tooltips = stringBuilder.ToString();
				}
			}
		}

		private void Chart_SizeChanged(object sender, EventArgs e)
		{
			ReloadPoint();
		}

		private void menuItem_Click(object sender, EventArgs e)
		{
			ToolStripMenuItem toolStripMenuItem = sender as ToolStripMenuItem;
			toolStripMenuItem.Checked = !toolStripMenuItem.Checked;
			ShowHotlidBlockTubeSeries(toolStripMenuItem.Checked);
		}

		public bool GetCursorVisible()
		{
			return itemShowCursor.Checked;
		}

		public int GetCursorValue()
		{
			return (int)seriesCursor.Points[0].ValueX;
		}

		private void Chart_MouseDown(object sender, MouseEventArgs e)
		{
			isMouseDown = true;
		}

		private void Chart_MouseUp(object sender, MouseEventArgs e)
		{
			if (seriesThreshold != null)
			{
				seriesThreshold = null;
				base.Chart.SetCursor(Cursors.Arrow);
				base.Chart.CanScale = true;
				base.Chart.ShowToolTips = true;
				base.Chart.DisplayAnnotation(false);
			}
			isMouseDown = false;
		}

		private string GetAnnotationText(double value)
		{
			return startDate.AddSeconds(value).ToLongTimeString();
		}

		private void Chart_MouseMove(object sender, MouseEventArgs e)
		{
			if (isMouseDown && seriesThreshold != null)
			{
				base.Chart.ShowToolTips = false;
				seriesThreshold.Points[0].ValueX = base.Chart.GetLocationXData(e.X);
				seriesThreshold.Points[1].ValueX = seriesThreshold.Points[0].ValueX;
				seriesThreshold.Visible = false;
				seriesThreshold.Visible = true;
				base.Chart.SetAnnotationText(GetAnnotationText(seriesCursor.Points[0].ValueX));
				RaiseCursorChangedChanged();
			}
		}

		private void Chart_OnSelectedSeriesChanged(object sender, SelectedSeriesChangedArgs e)
		{
			if (!isMouseDown)
			{
				int num = series.ToList().IndexOf(e.SelectedSeries as XYSeriesEx);
				SetSeriesHighLight(num);
				RaiseHighLightChanged(num);
				if (e.SelectedSeries == seriesCursor)
				{
					base.Chart.SetCursor(Cursors.VSplit);
					base.Chart.CanScale = false;
					seriesThreshold = seriesCursor;
					base.Chart.DisplayAnnotation();
					base.Chart.SetAnnotationText(GetAnnotationText(seriesCursor.Points[0].ValueX));
				}
				else
				{
					seriesThreshold = null;
					base.Chart.SetCursor(Cursors.Arrow);
					base.Chart.CanScale = true;
					base.Chart.DisplayAnnotation(false);
				}
			}
		}

		private void itemShowCursor_Click(object sender, EventArgs e)
		{
			if (!seriesCursor.Visible && (seriesCursor.Points[0].ValueX < base.Chart.GetCurrentMinX() || seriesCursor.Points[0].ValueX > base.Chart.GetCurrentMaxX()))
			{
				double valueX = base.Chart.GetCurrentMinX() + (base.Chart.GetCurrentMaxX() - base.Chart.GetCurrentMinX()) / 3.0;
				seriesCursor.Points[0].ValueX = valueX;
				seriesCursor.Points[1].ValueX = valueX;
			}
			itemShowCursor.Checked = !itemShowCursor.Checked;
			seriesCursor.Visible = itemShowCursor.Checked;
			RaiseCursorChangedChanged();
		}

		private void CalInterval()
		{
			if (listTempData.Count > 2)
			{
				double drawLayoutWidth = base.Chart.GetDrawLayoutWidth();
				double num = base.Chart.GetCurrentMaxX() - base.Chart.GetCurrentMinX();
				double num2 = listTwoTimeInterver.Average();
				interval = (int)Math.Ceiling(num * singlePix / (drawLayoutWidth * num2));
				if (interval < 1)
				{
					interval = 1;
				}
			}
		}

		private void Chart_OnAxixRangeChanged(object sender, EventArgs e)
		{
			ReloadPoint();
		}

		private void ReloadPoint()
		{
			CalInterval();
			for (int i = 0; i < series.Count(); i++)
			{
				if (listTempData.Count == 0)
				{
					series[i].Points.Clear();
					continue;
				}
				int num = 0;
				for (int j = 0; j < listTempData.Count(); j += interval)
				{
					XYDataPointCollection points = series[i].Points;
					if ((j + interval >= listTempData.Count || !(listTempData[j].Time.TotalSeconds < base.Chart.GetCurrentMinX()) || !(listTempData[j + interval].Time.TotalSeconds < base.Chart.GetCurrentMinX())) && (j + interval >= listTempData.Count || !(listTempData[j].Time.TotalSeconds > base.Chart.GetCurrentMaxX()) || !(listTempData[j + interval].Time.TotalSeconds > base.Chart.GetCurrentMaxX())))
					{
						if (num < points.Count)
						{
							points[num].ValueX = listTempData[j].Time.TotalSeconds;
							points[num].ValueY = listTempData[j].Temperature[i];
						}
						else
						{
							points.Add(new XYDataPoint(listTempData[j].Time.TotalSeconds, listTempData[j].Temperature[i], string.Empty, false));
						}
						num++;
					}
				}
				for (int num2 = series[i].Points.Count - 1; num2 >= num; num2--)
				{
					series[i].Points.RemoveAt(num2);
				}
				series[i].Points.Add(new XYDataPoint(listTempData[listTempData.Count - 1].Time.TotalSeconds, listTempData[listTempData.Count - 1].Temperature[i], string.Empty, false));
			}
		}

		public void Clear()
		{
			listTempData.Clear();
			listTwoTimeInterver.Clear();
			_HotlidTemperatureSeries.Points.Clear();
			_BlockTemperatureSeries.Points.Clear();
			_TubeTemperatureSeries.Points.Clear();
			for (int i = 0; i < series.Count(); i++)
			{
				series[i].Points.Clear();
			}
		}

		public void AddData(TimeSpan time, double[] temp, bool updateAxisX = false)
		{
			if (temp == null || temp.Count() != series.Count())
			{
				return;
			}
			listTempData.Add(new TemperatureData
			{
				Time = time,
				Temperature = temp
			});
			if (listTempData.Count >= 2)
			{
				listTwoTimeInterver.Add((listTempData[listTempData.Count - 1].Time - listTempData[listTempData.Count - 2].Time).TotalSeconds);
				CalInterval();
			}
			double totalSeconds = time.TotalSeconds;
			if (updateAxisX && totalSeconds > base.Chart.MaxX)
			{
				base.Chart.MaxX += 300.0;
			}
			for (int i = 0; i < temp.Count(); i++)
			{
				if (series[i].Points.Count < 2 || listTempData.Count % interval == 0)
				{
					series[i].Points.Add(new XYDataPoint(totalSeconds, temp[i], "", false));
					continue;
				}
				series[i].Points[series[i].Points.Count - 1].ValueX = totalSeconds;
				series[i].Points[series[i].Points.Count - 1].ValueY = temp[i];
			}
		}

		public void ShowSereies(int index, bool show = true)
		{
			if (index >= 0 && index < series.Count())
			{
				series[index].Visible = show;
			}
		}

		public void SetMaxX(TimeSpan time)
		{
			if (!(time.TotalSeconds < 300.0) && base.Chart.MaxX < time.TotalSeconds)
			{
				base.Chart.MaxX = time.TotalSeconds;
			}
		}

		public void SetSeriesHighLight(int index)
		{
			ISeries[] highLightSreis = base.Chart.GetHighLightSreis();
			foreach (ISeries series in highLightSreis)
			{
				series.PEs[0].Fill = defaultColor;
			}
			base.Chart.ClearHighLightSeries();
			if (index >= 0 && index < this.series.Count())
			{
				this.series[index].PEs[0].Fill = Color.Black;
				base.Chart.AddHighLightSeries(this.series[index]);
			}
		}
	}
}
